﻿
Imports SharpSvn
Imports System.Text.RegularExpressions

Module Module1

    Structure ArchivedFileData
        Implements IComparable

        Dim name As String
        Dim version As Long
        Dim path As String

        Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
            Return version.CompareTo(CType(obj, ArchivedFileData).version)
        End Function
    End Structure

    Sub Main()
        Dim client As New SvnClient()
        client.CheckOut(New Uri("https://github.com/shai0noy/shai-cook-book/trunk/WikiData/Pages"), "c:\temp\pages")
        Dim files = IO.Directory.GetFiles("C:\Users\Public\Documents\Synced\WikiData\Archive")
        Dim filesDataArray(files.Length - 1) As ArchivedFileData
        Dim fileNamePatt As New Regex(".*\\(.+?)\.(\d+?)\.xml")
        For i = 0 To files.Length - 1
            Dim match = fileNamePatt.Match(files(i))
            filesDataArray(i).path = files(i)
            filesDataArray(i).version = Long.Parse(match.Groups(2).Value)
            filesDataArray(i).name = match.Groups(1).Value
        Next
        Array.Sort(filesDataArray)
        For Each info In filesDataArray
            If info.version > 63547447803 Then
                addFile(client, "c:\temp\pages", info.path, info.name)
            End If
        Next
    End Sub

    Sub addFile(client As SvnClient, workingFolder As String, sourceFiie As String, targetName As String)
        Dim newFile As String = workingFolder & "\" & targetName & ".xml"
        IO.File.Copy(sourceFiie, newFile, True)
        Try
            client.Add(newFile)
        Catch ex As SvnEntryException
            'thats ok
        End Try

        Dim args As New SvnCommitArgs()
        args.LogMessage = "Auto Archive-To-SVN"
        client.Commit(newFile, args)
    End Sub

End Module
